﻿﻿ var sysNameSource = {
		 "22": {title:"公安警务信息",src : "framework/images/topbar/system22.png"},
		 "31": {title:"银行账户资金网络",src : "framework/images/topbar/system31.png"},
		 "32": {title:"不动产信息",src : "framework/images/topbar/system32.png"},
		 "23": {title:"环境噪声监测系统",src : "framework/images/bg/system23.png"},
		 //"39": {title:"扬尘在线监测系统",src : "framework/images/bg/system39.png"},
		 "51": {title:"工况系统-烟气",src : "framework/images/bg/system51.png"},
		 "52": {title:"工况系统-污水",src : "framework/images/bg/system52.png"},
		 "104":{title:"油烟在线监测系统",src : "framework/images/bg/system104.png"},
		 "105": {title:"无源扬尘在线监测系统",src : "framework/images/bg/system105.png"},
		 "9999":{title:"系统管理",src : "framework/images/topbar/system9999.png"},
//		 "10":{title:"排污许可证智能分析系统",src : "framework/images/bg/system10.png"},
//		 "20":{title:"污染源全生命周期信息查询系统",src : "framework/images/bg/system104.png"},
//		 "30":{title:"环境信息公开平台",src : "framework/images/bg/system23.png"},
//		 "40":{title:"许可证核发管理系统",src : "framework/images/system-admin.png"}
 };
//页面类定义，页面名字默认是该类名
var system = {		
     currentAlarmPage:1,//当前告警的页面    
     alarmPageSize:5,//告警的页面数据大小
     alarmTotalPage:0,//总页数
     loadCount:0,//加载次数
	//定义数据模型
	viewMode : {
			menuData : ko.observableArray(),
			children : ko.observableArray(),
			userName : ko.observableArray(), // 当前用户名
			systemType : ko.observableArray(), // 系统类型ID
			desktopMenu : ko.observableArray(),
			menuId : ko.observableArray(),
			nowTime : ko.observableArray(),
			alarmData:ko.observableArray()//告警提醒数据
		},
    init: function() { 
    	this.eventBind();
    	this.viewMode.systemType(getUrlParam("systemType"));
    	this.viewMode.nowTime((new Date()).format("yyyy-MM-dd hh:mm:ss"));
    	setInterval("system.refreshData()", 1000);
    	var menuId = getUrlParam("menuid");
    	if (menuId != null && menuId > 0){
    		this.viewMode.menuId(menuId);
    	}    	
    	if(this.viewMode.systemType()===null||this.viewMode.systemType()===""){
    		$(".n-lgpic #tiny").attr("src",sysNameSource["9999"].src);
    		$(".sysName").text(sysNameSource["9999"].title);
        	$(document).attr("title",sysNameSource["9999"].title);
    	}else{
    		console.log(100, this.viewMode.systemType());
    		console.log(101, sysNameSource[this.viewMode.systemType()]);
    		$(".n-lgpic #tiny").attr("src", sysNameSource[this.viewMode.systemType()].src);
    		$(".sysName").text(sysNameSource[this.viewMode.systemType()].title);
        	$(document).attr("title",sysNameSource[this.viewMode.systemType()].title);
    	}
    	
    	ko.applyBindings(this.viewMode);

    	this.getUserInfo();
    	this.getMenuInfo();   
    	this.getSysMenuInfo();
    	this.resetContentSize();
    },    
    eventBind: function() {
    	$("#frameMain").bind('load',function() {    		
    		$.unblockUI();    		
    	});  
    	$(".skinchange").bind('click',function(){
    		 $('#surface').window('open');
    		 var style=getCookie("cookieskin");
    		 $(".bomb-list li").removeClass("active");
    		 $("#"+style).addClass("active");
    	});
    	$(".btn-alarm").bind('click',function(){
    		system.initAlarmInfo();
    	});
    	$("#menulist").hover(function() {
    		$(".drop-down-menu").show(); 
    	}, function() {
    		$(".drop-down-menu").hide(); 
    		$(".drop-down-menu").hover(function() {
        		$(".drop-down-menu").show(); 
        	}, function() {
        		$(".drop-down-menu").hide(); 
        	}); 
    	});
    	$("#surface .skin").bind('click',function(){
    		var style = $(this).attr("id"); 
    	   
            ajaxProcess({
                url : getProjectUrl('/bfs/user/updateSkin'),
                data : {
                    skin : style
                },
                requestType : 'POST',
                traditional : true,
                successHandle : function(data) {
                	 $.each($("link[title='skin']"),function(i){
             	    	$(this).attr("href",$(this).attr("href").replace(/skin\/[0-9a-zAz]+\/styles/g, "skin/"+style+"/styles"));
             	    });
             	    var ifm= document.getElementById("frameMain").contentWindow.document ;
             	    $.each($(ifm).find("link[title='skin']"),function(i){
             	    	$(this).attr("href",$(this).attr("href").replace(/skin\/[0-9a-zAz]+\/styles/g, "skin/"+style+"/styles"));
             	    });
             	    setCookie("cookieskin",style);
             	    $(".bomb-list li").removeClass("active");
             	    $("#"+style).addClass("active");
                }
            });
    	});
    	
    	$(".right-move").bind('click',function(){
    		if($("#subMenuItem ul li:last.hide").length>0){
    			var menuwidth=$("#subMenuItem ul").width();
    			$("#subMenuItem ul li.show:first").removeClass("show").addClass("hide").hide();
    			$("#subMenuItem ul li.show:last").next().removeClass("hide").addClass("show").show();
    			var width=0;
    			$.each($("#subMenuItem ul li"),function(i){
    				if(!$(this).hasClass("hide")){
    					width+=$(this).width()+20;
    				}
    			});
    			if(width>menuwidth){
    				$("#subMenuItem ul li.show:first").removeClass("show").addClass("hide").hide();
    			}    			
    		}
    	});
    	$(".left-move").bind('click',function(){
    		if($("#subMenuItem ul li:first.hide").length>0){
    			$("#subMenuItem ul li.show:first").prev().removeClass("hide").addClass("show").show();
    			var width=0;
    			$.each($("#subMenuItem ul li"),function(i){
    				if(!$(this).hasClass("hide")){
    					width+=$(this).width()+20;
    					$(this).removeClass("hide").addClass("show");
    				}
    				if(width>=$("#subMenuItem ul").width()){
    					$(this).removeClass("show").addClass("hide").hide();
    				}
    			});
		}
    	});
    	
    	$("#deskHome").hover(function() {
    		$(".n-othstm").css("display", "block");
    	}, function() {
    		$(".n-othstm").css("display", "none");
    	});
    	$(".n-othstm").hover(function() {
    		$(".n-othstm").css("display", "block");
    	}, function() {
    		$(".n-othstm").css("display", "none");
    	});
    	
    	// 当窗口改变时变化
    	$(window).resize(function() {
    		system.resetContentSize();
    	});
    },
    resetContentSize:function () {
    	var mainh = document.documentElement.clientHeight;
    	var mainw = document.documentElement.clientWidth;
    	if(mainh===0){
    		mainh= document.body.clientHeight;
    		mainw=document.body.clientWidth;
    	}
    	$(".content").width(mainw - 5 + "px");
    	var ifm= document.getElementById("frameMain"); 
        ifm.height=mainh;
        ifm.width=mainw;
        var headHeight=145;
        if (document.documentElement.clientWidth <= 1024) {
        	headHeight=130;
        }
    	if (this.viewMode.children().length > 0) {
    		$("#subMenuItem").show();
    		$(".content").css("top", "108px");
    		$(".content").height((mainh- headHeight) + "px");
    	} else {
    		$("#subMenuItem").hide();
    		$(".content").height((mainh- headHeight + 60) + "px");
    		$(".content").css("top", "56px");
    	}

    	if (document.documentElement.clientWidth <= 1024) {
    		$(".content").css("top", "98px");
    		$(".content").height((mainh- headHeight) + "px");
    		$("#navBarLog").width(document.documentElement.clientWidth / 21 * 4);
    		$(".topbt").css("font-size", "16px");
    	} else {    		
    		$(".topbt").css("font-size", "20px");
    	}
    	$("#navBarMain").width("auto");
		//$("#navBarMenu a").css('padding-right', "8px");
		//$("#navBarMenu a").css('padding-left', "8px");
    	var width=0;
    	$("#subMenuItem ul li").removeClass("hide").show();
    	$(".menu-move").show();
		$.each($("#subMenuItem ul li"),function(i){
			if(!$(this).hasClass("hide")){
				width+=$(this).width()+20;
				$(this).removeClass("hide").addClass("show");
			}
			if(width>=$("#subMenuItem ul").width()){
				$(this).removeClass("show").addClass("hide").hide();
			}
		});
		if(width<$("#subMenuItem ul").width()){
			$(".menu-move").hide();
		}
		$(".icon-main").css("left",(document.documentElement.clientWidth-$(".icon-main").width())/2);
		$(".icon-main").css("top",(document.documentElement.clientHeight-$(".icon-main").height())/2);
		
		var menuWidth=$("#navBarMenu ul li").width();
		var menuLength=$("#navBarMenu ul li").length+$(".drop-down-menu ul li").length;
		var menuBarWidth=$("#navBarMenu ul").width();
		if(menuBarWidth/menuLength<86){
			if(menuBarWidth/menuLength<64){
				$("#navBarMenu ul li").width(64);
			}else{
				$("#navBarMenu ul li").width(menuBarWidth/menuLength);
			}			
		}else{
			$("#navBarMenu ul li").width(86);
		}
		/*if(($("#navBarMenu ul").width()-64)<menuWidth*menuLength){
			$(".n-nav").css("padding-right","190px");
			$("#menulist").show();
			var hidLength=menuLength-parseInt($("#navBarMenu ul").width()/menuWidth);
			for(var i=menuLength-1;i>=menuLength-hidLength;i--){
				$(".drop-down-menu ul").append($("#navBarMenu ul li")[i]);				
			}
		}else{
			$(".n-nav").css("padding-right","130px");
			$("#menulist").hide();
		}*/
    },
    getUserInfo:function () {
        var url = "/"+projectName+"/bfs/user/getUserInfo";       
        ajaxRequest(url, null, null, null,
            function(resp) {
        	    system.viewMode.userName(resp.username);
            },
            null);
    },
    getMenuInfo:function () {
        var url = "/"+projectName+"/bfs/menu/queryUserChildMenu"; 
        var data = {
        		systemType : this.viewMode.systemType(),        		
        		menuId: this.viewMode.menuId()
        	};
        ajaxRequest(url, null, data, null,
            function(resp) {
        		if(resp.rows === null || resp.rows.length === 0){
        			//加載一個頁面或者彈出
        			alert("对不起，你没有访问权限,请联系管理员。");
        		}else{
        			system.viewMode.menuData(resp.rows);
            	    system.mouseEvent();
            	    system.loadMainPageEvent(system.viewMode.menuData()[0].data.menuUrl, system.viewMode
        					.menuData()[0].children, 0, system.viewMode.menuData()[0].data.menuId);
        			$("#navBarMenu li")[0].click();
        			if (system.viewMode.menuData()[0].children != null
        					&& system.viewMode.menuData()[0].children.length > 0 && $("#subMenu a").length > 0) {
        					$("#subMenu a")[0].click();
        			}        
        		}
        		
        	   
            });
            	
    },
  //获取桌面菜单列表
    getSysMenuInfo: function(event) {
        var url = "/bfs/bfs/menu/queryFirstMenu";       
        ajaxRequest(url, null, null, null,
            function(data) {                
        	    if (isEmpty(data.rows)) {
        	    	return;
        	    }
        	    if (data.rows.length === 1) //如果只有一个子系统权限，自动跳转到该子系统
        	    {
        	    	$(".sysmenu").hide();
        	    	return;
        	    }
                system.viewMode.desktopMenu(data.rows);
                $(".deskicon li").bind("click",function(){
                	window.location ="system.html?systemType="+$(this).attr("id");
                });
            },
            null);
    },
    mouseEvent:function() {
    	// 菜单鼠标经过变色
    	$("#navBarMenu li").hover(function() {
    		if (!$(this).hasClass("current")) {
    			$(this).addClass("mouseOver");
    			$(this).find("a").css("color", "#fff");
    		}
    	}, function() {
    		$(this).removeClass('mouseOver');
    	});

    	// 绑定菜单点击事件
    	$("#navBarMenu li").bind('click', function() {
    		$("#navBarMenu li").each(function() {
    			$(this).removeClass('current');
    		});
    		//var posX = $(this).offset().left;
    		var menuName = $(this).find('div').text();

    			$("#subMenuItem").css({
    				"margin-left" : "0px"
    			});
    		if (system.viewMode.menuData() != null) { // 解决分辨率为笔记本的情况下子菜单显示不完全的问题
    			for (var i = 0; i < system.viewMode.menuData().length; i++) {
    				if (system.viewMode.menuData()[i].data.MenuName === menuName && system.viewMode.menuData()[i].SubMenus != null && system.viewMode.menuData()[i].SubMenus.length >= 8) {
    							$("#subMenuItem").css({
    								"left" : "50%",
    								"margin-left" : "-420px"
    							});   					
    					break;
    				}
    			}
    		}
    		system.resetContentSize();
    		$(this).addClass('current');
    	});

    	$("#subMenuItem a").bind('click', function() {
    		$("#subMenuItem a").each(function() {
    			$(this).removeClass('btn-success');
    			$(this).removeClass('active');
    		});
    		$(this).addClass('btn-success');
    		$(this).addClass('active');
    	});   	
    },
    loadMainPageEvent:function(tabUrl, SubMenus, type, menuId) {
    	if (type === 0) {
    		this.viewMode.children([]);
    	}
    	if (tabUrl !== "" && tabUrl != null) {
    		if (tabUrl.indexOf("?") > -1) {
    			tabUrl += "&menuid=" + menuId;
    		} else {
    			tabUrl += "?menuid=" + menuId
    		}
    		if (tabUrl.indexOf("?") > -1) {
    			tabUrl += "&systemType=" + this.viewMode.systemType();
    		} else {
    			tabUrl += "?systemType=" + this.viewMode.systemType()
    		}
    		if (tabUrl.indexOf("?") > -1) {
    			tabUrl += "&ver=" + Math.random();
    		} else {
    			tabUrl += "?ver=" + Math.random();
    		}
    	}

    	if (!(SubMenus === null || SubMenus.length < 1)) {    		
    		this.viewMode.children([]);
    		this.viewMode.children(SubMenus);
    		this.resetContentSize();
    		this.mouseEvent();
    		if($("#subMenuItem a").length>0){
        		$("#subMenuItem a")[0].click();
    		}
    		var width=0;
    		$.each($("#subMenuItem ul li"),function(i){
    			if(!$(this).hasClass("hide")){
    				width+=$(this).width()+20;
    				$(this).addClass("show");
    			}
    			if(width>=$("#subMenuItem ul").width()){
    				$(this).removeClass("show").addClass("hide").hide();
    			}
    		});
    		if(width<$("#subMenuItem ul").width()){
    			$(".menu-move").hide();
    		}
    		return;
    	}

    	$.blockUI({
    		message : "加载中,请稍候...",
    		fadeIn : 300,
    		fadeOut : 300,
    		css : {
    			width : '250px',
    			top : ($(window).height() - 100) / 2 + 'px',
    			left : ($(window).width() - 200) / 2 + 'px',
    			border : 'none',
    			padding : '5px',
    			backgroundColor : '#000',
    			'-webkit-border-radius' : '5px',
    			'-moz-border-radius' : '5px',
    			opacity : .7,
    			fontSize : '16px',
    			fontWeight : 'bold',
    			color : '#fff'
    		}
    	});

    	if(tabUrl !== null && tabUrl !== undefined && tabUrl.indexOf("bfs")>-1){
    		//tabUrl = "views\\" + tabUrl;
    	}
    	
    	$("#frameMain").attr("src", tabUrl);
    	
        
        if(system.viewMode.systemType()==="104" && system.loadCount==0){
            system.initAlarmInfo();
            ++system.loadCount;
        } 
    },
    //初始化告警的弹出窗口
    initAlarmInfo:function(){
        
        var dialogWidth = 560;      // dialog的宽度  
        var dialogHeight = 260;     // dialog的高度  
        var topPosition = $(window).height() - dialogHeight;  
        var leftPosition = $(window).width() - dialogWidth;  
        $('#alarmDiv').window({  
            width: dialogWidth,  
            height: dialogHeight,  
            top : topPosition,  
            left : leftPosition,  
            title:'告警提醒',
            openAnimation:'fade'
        });
        
        $("#alarm_view").bind('click',function(){
            var oneMenu;
            var subMenu;
            var navDivs=$("#navBarMenu li div");
            for(var i=0;i<navDivs.length;i++){
                var navText=$(navDivs[i]).text();
                if("告警管理"===navText){
                    oneMenu=$(navDivs[i]);
                    $(oneMenu)[0].click();
                    break;
                }
            }
            var subMenuAs=$("#subMenuItem li a");
            for(var j=0;j<subMenuAs.length;j++){
                var subMenuText=$(subMenuAs[j]).text();
                if("实时告警"===subMenuText){
                    subMenu=$(subMenuAs[j]);
                    $(subMenu)[0].click();
                    break;
                }
            }
        });
        
       system.getAlarmData(system.currentAlarmPage,system.alarmPageSize);
       
       system.bindPageEvent();
       
    },
    //获取告警信息
    getAlarmData:function(pageNum,pageSize){
        ajaxProcess({
            url:'/gems/gems/realtimealarm/home/alarmlist',
            requestType : 'POST',
            data:{
                page:pageNum,
                rows:pageSize
            },
            async:false,
            successHandle : function(data) {
                if(data.rows === null || data.rows.length === 0){
                    $('#alarmDiv').window('close');
                } else {
                	document.getElementById("sound").src="framework/music/alarm.wav";
                    $('#alarmDiv').window('open');
                    system.viewMode.alarmData(data.rows);
                    var alaramTotal=data.total;
                    system.alarmTotalPage=(alaramTotal%system.alarmPageSize===0?alaramTotal/system.alarmPageSize:parseInt(alaramTotal/system.alarmPageSize)+1);
                    var showPageText=pageNum+"/"+system.alarmTotalPage;
                    $("#showPageText").text(showPageText);
                    system.currentAlarmPage=pageNum;
                }
            }
        });
    },
    //分页事件绑定
    bindPageEvent:function(){
        $("#homePage").bind('click',function(){
            if(system.currentAlarmPage!=1){
                system.getAlarmData(1,system.alarmPageSize);
            }
        });
        
        $("#prePage").bind('click',function(){
            if(system.currentAlarmPage>1){
                system.getAlarmData(system.currentAlarmPage-1,system.alarmPageSize);
            }
        });
        
        $("#nextPage").bind('click',function(){
            if(system.currentAlarmPage<system.alarmTotalPage){
                system.getAlarmData(system.currentAlarmPage+1,system.alarmPageSize);
            }
        });
        
        $("#endPage").bind('click',function(){
            if(system.currentAlarmPage!=system.alarmTotalPage){
                system.getAlarmData(system.alarmTotalPage,system.alarmPageSize);
            }
        });
    },
    // 定时刷新
	refreshData : function() {
		this.viewMode.nowTime((new Date()).format("yyyy-MM-dd hh:mm:ss"));
	}
};

function showModal(){
	$('.bomb-content').show();
	}
	
function hideModal(){
	$('.bomb-content').hide();
	}